package org.funtester.performance.books.chapter08.section2;

import com.funtester.fungrpc.QueryRequest;
import com.funtester.fungrpc.QueryResponse;
import com.funtester.fungrpc.UserServiceGrpc;
import com.google.common.util.concurrent.ListenableFuture;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.funtester.performance.books.chapter03.common.ThreadTool;

public class NonBlockingClient {

    public static void main(String[] args) {
        ManagedChannel managedChannel = ManagedChannelBuilder// 创建连接builder对象
                .forAddress("127.0.0.1", 8080)// 指定地址和端口
                .usePlaintext()// 使用明文传输,不使用ssl安全连接
                .build();// 创建连接

        UserServiceGrpc.UserServiceFutureStub userServiceFutureStub = UserServiceGrpc.newFutureStub(managedChannel);// 创建异步存根
        QueryRequest queryRequest = QueryRequest.newBuilder().setId(888).build();// 创建请求对象
        ListenableFuture<QueryResponse> queryResponseFuture = userServiceFutureStub.queryUser(queryRequest);// 发送请求
        queryResponseFuture.addListener(() -> {// 添加监听器
            try {
                QueryResponse queryResponse = queryResponseFuture.get();// 获取响应
                System.out.println(queryResponse);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, Runnable::run);
        ThreadTool.sleep(300);
    }

}
